<?php

/**
 * ERP用户仓库
 * @author wangwx 2015-5-12
 */
namespace common\repositories\myhtsq;
use common\repositories\BaseRepository;
use common\support\StringHelper;
use common\entities\myhtsq\ErpUserEntity;
use yii\db\Query;
class ErpUserRepository extends BaseRepository{

	/**
	 * 构造方法
	 * @param unknown $config (optional)
	 */
	public function __construct($config = [])
	{
		parent::__construct($config);
	}
	
	/**
	 * 根据关键字,搜索返回用户列表
	 * @param string $keyword 搜索关键字
	 * @param int $limit 返回条数
	 * @return array
	 */
	public static function getErpUserByKeyword($keyword, $limit = 10)
	{
		$query = (new Query())->from(ErpUserEntity::tableName())
				->select('ParentBU.BUCode,myscrm_erp_user.UserGUID,myscrm_erp_user.UserCode')
				->addSelect('myscrm_erp_user.UserName,myscrm_erp_user.BUGUID,myscrm_erp_user.DepartmentGUID')
				->addSelect('CompanyBu.BUName as CompanyName,DepartmentBu.BUName as DepartmentName')
				->addSelect('myscrm_erp_user.AbuName,myscrm_erp_user.AbuFzrName,myscrm_erp_user.DeptmentFzrName')
				->addSelect('myscrm_erp_user.AbuGUID,myscrm_erp_user.ABuFzrID,myscrm_erp_user.DeptmentFzrID')
				->leftJoin('myscrm_erp_business_unit ParentBU', 'ParentBU.BUGUID=myscrm_erp_user.ParentGUID')
				->leftJoin('myscrm_erp_business_unit CompanyBu', 'CompanyBu.BUGUID=myscrm_erp_user.BUGUID')
				->leftJoin('myscrm_erp_business_unit DepartmentBu', 'DepartmentBu.BUGUID=myscrm_erp_user.DepartmentGUID')
				->where(['myscrm_erp_user.IsDisabeld' => 0]);
		if (!empty($keyword)) {
			$query->andWhere(['like', 'myscrm_erp_user.UserName', $keyword]);
		}
		return $query->limit($limit)->all(ErpUserEntity::getDb());
	}
	
	
	/**
	 * 根据用户id,获取用户名称
	 * @param string $userGuid 用户guid
	 * @return string $userName 用户名称
	 */
	public static function getUserNameByBuguid($userGuid) {
		$res = ErpUserEntity::find()->select('UserName')->where(['UserGUID'=>$userGuid])->scalar();
        return empty($res) ? '' : $res;
	}

    /**
     * 根据用户名称,获取用户id
     * @return string $userName 用户名称
     */
    public static function getUserGUIDByUserName($userName) {
        $res = ErpUserEntity::find()->select('UserGUID')->where(['UserName'=>$userName])->scalar();
        return empty($res) ? '' : $res;
    }
        
    /**
    * 根据用户代码获取用户实体信息
    * @param string $usercode
    * @return ErpUserEntity
    */
	public static function getUserByCode($usercode) {
		return ErpUserEntity::find()->where(['UserCode'=>$usercode])->one();
	}	

    /**
    * 根据用户Id获取用户实体信息
    * @param string $userguid
    * @return ErpUserEntity
    */
	public static function getUserById($userguid) {
		return ErpUserEntity::find()->where(['UserGuid'=>$userguid])->one();
	}

    /**
     * 根据用户Id获取用户邮箱
     * @param string $userguid
     * @return bool|null|string
     */
    public static function getUserEmailByGUID($GUID){
        return ErpUserEntity::find()->select('Email')->where(['UserGUID' => $GUID])->createCommand()->queryScalar();
    }
        
    /**
    * 根据用户代码和密码获取用户实体信息
    * @param string $usercode
    * @param string $password
    * @return ErpUserEntity
    */
	public static function getUserByCodeAndPassword($usercode,$password) {
		return ErpUserEntity::find()->where(['UserCode'=>$usercode,'Password'=>$password])->one();
	}

	public static function getBuguidByUserId($userid){
		return ErpUserEntity::find()->where(['UserGUID'=>$userid])->one();
	}

    /**
     * 根据用户Id获取用户实体信息
     * @param string $userguid
     * @return ErpUserEntity
     */
    public static function getUserInfoById($userguid) {
        $query = (new Query())->from(ErpUserEntity::tableName())
            ->select('ParentBU.BUCode,myscrm_erp_user.UserGUID,myscrm_erp_user.UserCode')
            ->addSelect('myscrm_erp_user.UserName,myscrm_erp_user.BUGUID,myscrm_erp_user.DepartmentGUID')
            ->addSelect('CompanyBu.BUName as CompanyName,DepartmentBu.BUName as DepartmentName')
            ->addSelect('myscrm_erp_user.AbuName,myscrm_erp_user.AbuFzrName,myscrm_erp_user.DeptmentFzrName')
            ->addSelect('myscrm_erp_user.AbuGUID,myscrm_erp_user.ABuFzrID,myscrm_erp_user.DeptmentFzrID')
            ->leftJoin('myscrm_erp_business_unit ParentBU', 'ParentBU.BUGUID=myscrm_erp_user.ParentGUID')
            ->leftJoin('myscrm_erp_business_unit CompanyBu', 'CompanyBu.BUGUID=myscrm_erp_user.BUGUID')
            ->leftJoin('myscrm_erp_business_unit DepartmentBu', 'DepartmentBu.BUGUID=myscrm_erp_user.DepartmentGUID')
            ->where(['myscrm_erp_user.IsDisabeld' => 0]);
        if (!empty($userguid)) {
            $query->andWhere(['myscrm_erp_user.UserGUID' => $userguid]);
        }
        return $query->one(ErpUserEntity::getDb());
    }
}